package org.techmore.hakka;

import org.techmore.bussiness.MainActivityDataHandler;
import org.techmore.common.utils.activity.ActivityUtils;
import org.techmore.common.utils.sound.SoundPoolUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
	
	private LinearLayout visitorGame = null;
	private LinearLayout visitorVoice = null;
	
	private AlertDialog confirmDialog = null;
	private PopupWindow mainPopupWindow = null;
	
	private LinearLayout mainactivityid = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		ActivityUtils.getInstance().setScreenOrientation(this);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.mainactivity);
		
		mainactivityid = (LinearLayout) findViewById(R.id.mainactivityid);
		
		visitorGame = (LinearLayout) this.findViewById(R.id.visitorGame);
		visitorVoice = (LinearLayout) this.findViewById(R.id.visitorVoice);
		
		initAlertDialog();
		
		setButtonTouch();
	}
	
	private void setButtonTouch(){
		
		visitorGame.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(MotionEvent.ACTION_DOWN == event.getAction()){
					SoundPoolUtils.getInstance(MainActivity.this).playSound(SoundPoolUtils.SOUND_BUTTON_CLICK);
					ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, MainActivity.this, ChallengeMainActivity.class);
				}
				
				return false;
			}
		});
		
		visitorVoice.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(MotionEvent.ACTION_DOWN == event.getAction()){
					SoundPoolUtils.getInstance(MainActivity.this).playSound(SoundPoolUtils.SOUND_BUTTON_CLICK);
				}
				confirmDialog.show();
				return false;
			}
		});
	}
	
	
	private void initAlertDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.main_activity_alertdialog_warn);
		builder.setMessage(R.string.main_activity_alertdialog_content);
		builder.setNegativeButton(R.string.main_activity_alertdialog_cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		if(null != mainPopupWindow){
			mainPopupWindow.dismiss();
		}
		builder.setPositiveButton(R.string.main_activity_alertdialog_confirm, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				confirmDialog.dismiss();
				initProcessDialog();
				mainPopupWindow.showAtLocation(mainactivityid, Gravity.CENTER, 0, 0);
				
				//取得定位点
				MainActivityDataHandler.getInstance().getLocation(MainActivity.this,MainActivity.this,mainPopupWindow);
				
				
			}
		});
		confirmDialog = builder.create();
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(null != data){
			//-------------切入點--------------
			System.out.println("geoPointString:"+data.getStringExtra("geoPointString"));
		}
	}
	
	
	private void initProcessDialog(){
		View view = LayoutInflater.from(this).inflate(R.layout.processbar_mainactivity, null);
//		view.getBackground().setAlpha(120); //0-255
		mainPopupWindow = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mainPopupWindow.setAnimationStyle(R.style.PopupAnimation);
	}
	
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	confirmDialog = null;
    	mainPopupWindow = null;
    }
}
